package com.yuncheng.oaApi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.common.base.service.impl.BaseServiceImpl;
import com.yuncheng.oaApi.constant.OaLogConstant;
import com.yuncheng.oaApi.constant.OaSpcyConstant;
import com.yuncheng.oaApi.entity.OaSpcyJcyFybxdgrjl;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.oaApi.mapper.OaYcSysUserMapper;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.oaApi.service.IOaYcSysUserService;
import com.yuncheng.vo.GetKsKz;
import com.yuncheng.oaApi.vo.PageVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OaYcSysUserServiceImpl extends BaseServiceImpl<OaYcSysUserMapper, YcSysUser> implements IOaYcSysUserService {

    @Resource
    private OaYcSysUserMapper oaYcSysUserMapper;

    @Resource
    private ISpcyLogService spcyLogService;



    @Override
    public YcSysUser getYcSysUserById(String userId) {
        YcSysUser ycSysUser = getById(userId);
        if (ycSysUser != null){
            return ycSysUser;
        }
        return null;
    }

    @Override
    public List<GetKsKz> getKsKzObject(String deptName, String zw) {
        List<GetKsKz> ksKzObject = oaYcSysUserMapper.getKsKzObject(deptName, zw);
        return ksKzObject;
    }

    @Override
    public List<GetKsKz> getKsKzObjectzbm(String deptName, String zw) {
        List<GetKsKz> ksKzObject = oaYcSysUserMapper.getKsKzObjectzbm(deptName, zw);
        return ksKzObject;
    }

    @Override
    public String getUserZbmByUserId(String userid) {
        List<String> list = oaYcSysUserMapper.getUserZbmByUserId(userid);
        if (CollectionUtil.isNotEmpty(list)){
            return list.get(0);
        }

        return "0";
    }

    @Override
    public List<YcSysUser> selectUserBySfzh(String sfzh) {
        return oaYcSysUserMapper.selectList(new LambdaQueryWrapper<YcSysUser>()
                .eq(YcSysUser::getFsfzh, sfzh));
    }


    @Override
    public Page<YcSysUser> getFybxdUserPageList(YcSysUser query, String gjz, PageVo pageVo) {
        Page<YcSysUser> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        LambdaQueryWrapper<YcSysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(org.apache.commons.lang3.StringUtils.isNotBlank(query.getRealname()), YcSysUser::getRealname, query.getRealname());
        queryWrapper.like(org.apache.commons.lang3.StringUtils.isNotBlank(query.getFgzdw()), YcSysUser::getFgzdw, query.getFgzdw());
        queryWrapper.eq(YcSysUser::getStatus, 1);
        queryWrapper.eq(YcSysUser::getDelFlag, 0);
        queryWrapper.isNotNull(YcSysUser::getRealname);

        if (org.apache.commons.lang3.StringUtils.isNotBlank(gjz)){

            int selectSex = 3;
            if (gjz.contains("男")){
                selectSex = 1;
            }else if (gjz.contains("女")){
                selectSex = 2;
            }
            Integer finalSelectSex = selectSex;

            queryWrapper
                    .and(f->f.like(YcSysUser::getRealname, gjz)
                            .or()
                            .like(YcSysUser::getFsfzh, gjz)
                            .or()
                            .like(YcSysUser::getFgzdw, gjz)
                            .or()
                            .like(YcSysUser::getFsxzy, gjz)
                            .or()
                            .like(YcSysUser::getFxl, gjz)
                            .or()
                            .like(YcSysUser::getPhone, gjz)
                            .or()
                            .eq(YcSysUser::getSex, finalSelectSex)
                    );
        }
        queryWrapper.orderByDesc(YcSysUser::getCreateTime);

        return oaYcSysUserMapper.selectPage(page, queryWrapper);
    }


    @Override
    public List<YcSysUser> getDeptIndexUserList(String sysorgid) {
        return oaYcSysUserMapper.getDeptIndexUserList(sysorgid);
    }

    @Override
    public void updateSysYhkOrGwk(OaSpcyJcyFybxdgrjl oldBean, OaSpcyJcyFybxdgrjl bxdryBean) {
        if (StringUtils.isBlank(oldBean.getFuserid())){
            return;
        }

        boolean isFlag = false;
        String logNr = "";

        YcSysUser updateBean = new YcSysUser();
        if (StringUtils.isNotBlank(bxdryBean.getFkfh())){
            updateBean.setFkfh(bxdryBean.getFkfh());
            isFlag = true;
            logNr += "开户行：";
            logNr += "从["+ oldBean.getFkfh() +"]设置为"+bxdryBean.getFkfh();
        }
        if (StringUtils.isNotBlank(bxdryBean.getFyhkh())){
            updateBean.setFyhkh(bxdryBean.getFyhkh());
            isFlag = true;
            logNr += "银行卡号：";
            logNr += "从["+ oldBean.getFyhkh() +"]设置为"+bxdryBean.getFyhkh();
        }
        if (StringUtils.isNotBlank(bxdryBean.getFkhhszd())){
            updateBean.setFkhhszd(bxdryBean.getFkhhszd());
            isFlag = true;
            logNr += "开户行所在地：";
            logNr += "从["+ oldBean.getFkhhszd() +"]设置为"+bxdryBean.getFkhhszd();
        }

        if (StringUtils.isNotBlank(bxdryBean.getFgwkh())){
            updateBean.setFgwkh(bxdryBean.getFgwkh());
            isFlag = true;
            logNr += "公务卡号：";
            logNr += "从["+ oldBean.getFgwkh() +"]设置为"+bxdryBean.getFgwkh();
        }
        if (StringUtils.isNotBlank(bxdryBean.getFgwkkhh())){
            updateBean.setFgwkkhh(bxdryBean.getFgwkkhh());
            isFlag = true;
            logNr += "公务卡开户行：";
            logNr += "从["+ oldBean.getFgwkkhh() +"]设置为"+bxdryBean.getFgwkkhh();
        }
        if (StringUtils.isNotBlank(bxdryBean.getFgwkszd())){
            updateBean.setFgwkszd(bxdryBean.getFgwkszd());
            isFlag = true;
            logNr += "公务卡所在地：";
            logNr += "从["+ oldBean.getFgwkszd() +"]设置为"+bxdryBean.getFgwkszd();
        }
        if (isFlag) {
            LambdaUpdateWrapper<YcSysUser> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(YcSysUser::getId, bxdryBean.getFuserid());

            oaYcSysUserMapper.update(updateBean, updateWrapper);

            spcyLogService.addLogs(OaLogConstant.LOGS_JCYGL,logNr,oldBean.getFuserid(),"检查员报销同步修改用户信息", OaSpcyConstant.LOGS_SYS_TYPE_XG);
        }
    }

    @Override
    public List<YcSysUser> findRealnameListByIds(List<String> ids) {
        return oaYcSysUserMapper.findRealnameListByIds(ids);
    }

    @Override
    public String findRealnameById(String id) {
        return oaYcSysUserMapper.findRealnameById(id);
    }

}
